package com.example.academic_affairs_system.model.dto.user;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.example.academic_affairs_system.exception.SystemException;
import com.example.academic_affairs_system.model.entity.AasClassStudent;
import com.example.academic_affairs_system.model.entity.AasUser;
import com.example.academic_affairs_system.utils.PasswordSecurity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 添加学生 dto
 *
 * @author hjx
 * @since 2023/02/03 10:41 AM
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("增加学生所需要的信息")
public class HandlerStudentDTO {
  @ApiModelProperty("学生的姓名")
  String nickname;

  @ApiModelProperty("性别")
  Integer sex;

  @ApiModelProperty("学生的身份证")
  String idCard;

  @ApiModelProperty("学生的联系方式")
  String concat;

  @ApiModelProperty("学生家庭的联系方式")
  String familyConcat;

  @ApiModelProperty("籍贯")
  String origin;

  @ApiModelProperty("学生的联系地址")
  String address;

  @ApiModelProperty("学生的 id")
  Long studentId;

  @ApiModelProperty("班级的 id")
  Long classId;

  @ApiModelProperty("职位")
  String position;

  @ApiModelProperty("职位性质")
  String positionDesc;

  @ApiModelProperty("评价")
  String rating;

  public boolean editStudentClass() {
    return classId != null
        || StrUtil.isNotBlank(rating)
        || StrUtil.isNotBlank(position)
        || StrUtil.isNotBlank(positionDesc);
  }

  public boolean editStudentUser() {
    return StrUtil.isNotBlank(nickname) || StrUtil.isNotBlank(idCard) || StrUtil.isNotBlank(concat);
  }

  public AasUser buildUserStudent(boolean generatePwd) {
    AasUser aasUser = new AasUser();
    if (StrUtil.isNotBlank(idCard)) {
      if (!IdcardUtil.isValidCard(idCard)) {
        throw new SystemException(-999, "身份证格式不正确", "请输入正确的身份证格式");
      }
      if (idCard.length() < 18) {
        idCard = IdcardUtil.convert15To18(idCard);
      }
      aasUser.setIdCard(idCard);
      if (generatePwd) {
        // 获取身份证后6位
        aasUser.setPassword(PasswordSecurity.generatedPassword(idCard.substring(12).toUpperCase()));
      }
    }
    aasUser.setConcat(concat);
    aasUser.setNickname(nickname);
    aasUser.setAddress(address);
    aasUser.setAge(IdcardUtil.getAgeByIdCard(idCard));
    aasUser.setFamilyConcat(familyConcat);
    aasUser.setOrigin(origin);
    if (sex != null) {
      aasUser.setSex(sex.equals(1));
    }
    return aasUser;
  }

  public AasUser buildGenerateStudent(String studentNumber) {
    AasUser aasUser = buildUserStudent(true);
    aasUser.setUsername(studentNumber);
    aasUser.setCreateTime(LocalDateTimeUtil.now());
    return aasUser;
  }

  public AasClassStudent buildClassStudent(AasUser aasUser, long classId) {
    AasClassStudent classStudent = new AasClassStudent();
    // 获取身份证后6位
    classStudent.setStudentId(aasUser.getId());
    classStudent.setClassId(classId);
    classStudent.setPosition(position);
    classStudent.setPositionDesc(positionDesc);
    return classStudent;
  }
}
